<template>
  <div class="charts_container" ref="echartsPoly"></div>
</template>

<script>
export default {
  name: 'powerVsShift',
  data () {
    return {
      chartInstance: null,
      initOption: null
    }
  },
  created () {},
  mounted () {
    this.initChart()
  },
  props: {
    chartsData: {
      type: Array,
      required: true
    },
    whoUse: {
      type: String
    }
  },
  watch: {
    chartsData: {
      handler (newVal, oldVal) {
        if (oldVal !== newVal && oldVal !== undefined) {
          if (newVal !== []) {
            this.realData = newVal
            var copys = JSON.parse(JSON.stringify(this.realData))
            const seriesGroup = []
            var that = this
            seriesGroup[0] = {
              name: '锅数',
              type: 'bar',
              xAxisIndex: 0,
              yAxisIndex: 0,
              barWidth: 4,
              itemStyle: {
                normal: {
                  barBorderRadius: 5,
                  color: new that.$echarts.graphic.LinearGradient(0, 0, 0, 1, [
                    { offset: 0, color: 'rgb(0,255,255)' },
                    { offset: 1, color: 'rgb(0,108,237)' }
                  ])
                }
              },
              encode: {
                x: 0,
                y: 1
              }
            }
            seriesGroup[1] = {
              name: '总重',
              type: 'line',
              smooth: true,
              showAllSymbol: false,
              symbol: 'circle',
              symbolSize: 3,
              xAxisIndex: 0,
              yAxisIndex: 1,
              itemStyle: {
                normal: {
                  color: 'rgb(6,253,188)'
                }
              },
              encode: {
                x: 0,
                y: 2
              }
            }
            this.updateChart(seriesGroup, copys)
          }
        }
      },
      immediate: true
    }
  },
  beforeDestroy () {
    window.removeEventListener('resize', () => {
      this.chartInstance.resize()
    })
  },
  methods: {
    initChart () {
      this.initOption = {
        tooltip: {
          trigger: 'axis',
          backgroundColor: 'rgba(255,255,255,0.1)',
          axisPointer: {
            type: 'shadow',
            label: {
              show: true,
              backgroundColor: '#7B7DDC'
            }
          }
        },
        legend: {
          data: ['锅数', '总重'],
          textStyle: {
            color: '#B4B4B4'
          },
          top: '0%'
        },
        grid: {
          x: '7%',
          width: '86%',
          y: '12%',
          bottom: '10%',
          right: '4%',
          top: '15%'
        },
        xAxis: {
          type: 'category',
          axisLine: {
            lineStyle: {
              color: 'rgb(154, 170, 203)'
            }
          },
          nameTextStyle: {
            color: 'rgb(154, 170, 203)'
          },
          axisTick: {
            show: false
          },
        },
        yAxis: [
          {
            name: '锅数(个)',
            type: 'value',
            splitLine: { show: false },
            axisLine: {
              lineStyle: {
              }
            },
            nameTextStyle: {
              color: 'rgb(154, 170, 203)'
            },
            axisTick: { // 不显示轴刻度
              show: false
            },
            axisLabel: {
              color: 'rgb(154, 170, 203)',
              formatter: '{value}'
            }
          },
          {
            name: '总重(kg)',
            type: 'value',
            splitLine: { show: false },
            axisLine: {
              lineStyle: {
              }
            },
            nameTextStyle: {
              color: 'rgb(154, 170, 203)'
            },
            axisTick: { // 不显示轴刻度
              show: false
            },
            axisLabel: {
              formatter: '{value}',
              color: 'rgb(154, 170, 203)'
            }
          }
        ],
        series: [],
        dataset: {
          source: []
        }
      }
      this.chartInstance = this.$echarts.init(this.$refs.echartsPoly)
      // console.log('温度曲线initOption', this.initOption)
      this.chartInstance.setOption(this.initOption)
      this.echartsResize()
    },
    updateChart (seriesGroup, realData) {
      var gridShow = {
        x: '7%',
        width: '80%',
        y: '12%',
        bottom: '10%',
        right: '4%',
        top: '15%'
      }
      const dataOption = {
        series: seriesGroup,
        grid: gridShow,
        dataset: {
          source: realData // 数组
        }
      }
      this.chartInstance.setOption(dataOption)
    },
    echartsResize () {
      window.addEventListener('resize', () => {
        this.chartInstance.resize()
      })
    },
  }
}
</script>

<style  scoped>
.charts_container {
  width: 100%;
  height: 100%;
}
</style>
